<?php

namespace app\admin\validate\system;

use app\common\model\SystemRegion;
use app\common\basic\BaseValidate;

/**
 * 菜单验证器
 */
class SystemRegionValidate extends BaseValidate
{
    /**
     * 定义验证规则
     */
    protected $rule =   [
        'name' => 'require|checkName',
    ];

    /**
     * 定义错误信息
     */
    protected $message  =   [
        'name.require' => '名称必须填写',
        'name.checkName' => '已存在相同名称',
    ];
    
    public function checkName($value, $rule, $data = [])
    {
        $region = new SystemRegion();
        $where = [
            ['name', '=', $value],
            ['parent_id', '=', $data['parent_id']],
        ];
        if (!empty($data['id'])) {
            $where[] = ['id', '<>', $data['id']];
        }
        $regionList = $region->where($where)->findOrEmpty();
        if (!$regionList->isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * 定义场景
     */
    protected $scene = [
        'save' => [
            'name',
        ],
        'update' => [
            'name',
        ],
    ];

}
